package src.ee.ioc.smaug.ui;

import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.UIManager;

import src.ee.ioc.smaug.ui.evolver.WEvolver;


import java.awt.event.*;
import java.awt.*;

import java.io.File;

public class WMain extends JFrame implements ActionListener {
    public static JDesktopPane DMain;

    public static WEvolver evolver;

    public WMain() {
        super("SMAuG-State Machine Generator");

        //Make the big window be indented 50 pixels from each edge
        //of the screen.
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height
                - inset * 2);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(WindowEvent winEvt) {
                          System.exit(0); 
            }
        });
        //Set up the GUI.
        DMain = new JDesktopPane(); //a specialized layered pane
        DMain.setBackground(Color.GRAY);

        setContentPane(DMain);
        setJMenuBar(createMenuBar());

        //Make dragging a little faster but perhaps uglier.
        DMain.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        openEvolver();
        DMain.getDesktopManager().activateFrame(evolver);

    }

    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        //Set up the lone menu.
        JMenu menu = new JMenu("Open");

        menuBar.add(menu);

        //Set up the first menu item.
        JMenuItem menuItem = new JMenuItem("Evolver");
        menuItem.setActionCommand("evolver");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("Data generator");
        menuItem.setActionCommand("generator");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        //Set up the second menu item.
        menuItem = new JMenuItem("Quit");
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        return menuBar;
    }

    //  React to menu selections.
    public void actionPerformed(ActionEvent e) {
        if ("evolver".equals(e.getActionCommand())) { //new
            openEvolver();
        } else { //quit
            quit();
        }
    }

    //Create a new internal frame.
    protected void openEvolver() {
        evolver = new WEvolver();
        DMain.add(evolver);
        evolver.setVisible(true); //necessary as of 1.3

        try {
            evolver.setSelected(true);
            evolver.toFront();
            evolver.getFocusOwner();
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    //Quit the application.
    protected void quit() {
          System.exit(0);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.

        //Create and set up the window.
        WMain frame = new WMain();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Display the window.
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
         
            }
        });
    }
}
